package com.cloudkinto.service.job.impl;

import cn.hutool.core.date.DateUtil;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.entity.AsyncJobDo;
import com.cloudkinto.dao.AsyncJobDao;
import com.cloudkinto.entity.PlatformStoreDo;
import com.cloudkinto.service.job.AsyncJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.service.platform.vo.store.PlatformStoreRes;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 异步任务中心任务表 service实现类
 * </p>
 * @author long
 * @since 2025-06-23
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AsyncJobServiceImpl extends ServiceImpl<AsyncJobDao, AsyncJobDo> implements AsyncJobService {

    //每天定时删除 库存回传 10天之前的数据
    @Override
    public void deleteStockToPlatformRecord() {
        baseMapper.delete(new QueryWrapper<AsyncJobDo>().eq("job_type", "STOCK_TO_PLATFORM_PRODUCT")
                .lt("create_time", DateUtils.addOneDay(DateUtil.format(new Date(), "yyyy-MM-dd"), -10)));
    }

    @Override
    public void updateRows(Long id, Integer status, int rows, String errorMsg) {
        baseMapper.updateRows(id, status, rows, errorMsg);
    }

    @Override
    public void updateFinish(Long id, String resultUrl) {
        baseMapper.updateFinish(id, resultUrl);
    }


    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<AsyncJobDo> wrapper = queryBuild(map);
        IPage<AsyncJobDo> page = pageInit(map);
        page = this.page(page, wrapper);
        PageResult result = new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    private QueryWrapper<AsyncJobDo> queryBuild(Map<String, String> map) {
        QueryWrapper<AsyncJobDo> wrapper = new QueryWrapper<>();
        wrapper.eq("job_type", map.get("jobType"));
        wrapper.eq("company_id", map.get("companyId"));
        wrapper.eq(StringUtils.isNotBlank(map.get("storeId")), "store_id", map.get("storeId"));
        //beginTime 和endTime
        if (StringUtils.isNotBlank(map.get("beginTime")) && StringUtils.isNotBlank(map.get("endTime"))) {
            wrapper.ge("create_time", map.get("beginTime"));
            //map.get("endTime") yyyy-MM-dd格式 往后推一天
            wrapper.lt("create_time", DateUtils.addOneDay(map.get("endTime"), 1));
        }
        else {
            //默认近30天
            wrapper.ge("create_time", DateUtils.addOneDay(DateUtil.format(new Date(), "yyyy-MM-dd"), -10));
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }
    private IPage<AsyncJobDo> pageInit(Map<String, String> map) {
        IPage<AsyncJobDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

}
